<?php

namespace app\api\controller;

use app\api\business\User;
use app\api\validate\PhoneNumberValidate;
use app\BaseController;
use app\common\lib\Status;
use app\common\lib\Time;

class Login extends BaseController
{
    public function token()
    {
        $token = md5('15625707754' .time());
        echo $token;
    }

    public function number()
    {
        if (request()->isPost()) {
            // 获取用户手机号、验证码信息
            $input = [
                "phone_number" => input('post.phone_number'),
                'code' => input('post.code'),
                'type' => input('post.type')
            ];
            $validate = new PhoneNumberValidate();
            if (!$validate->scene('userlogin')->check($input)) {
                return Status::show(config('status.error'), $validate->getError());
            }

            // 将用户数据传business层中
            try {
                $UserBusiness = new User();
                $result = $UserBusiness->userLogin($input);
            } catch (\Exception $e) {
                return Status::show(config('status.success'), $e->getMessage());
            }
            
            return Status::show(config('status.success'), '登录成功！', $result);
        } else {
            return Status::show(config('status.error'), '请求类型错误哟！');
        }
    }
}
